// pages/record.js
const { ledgerConfig, ledgerTools } = require('../../utils/ledgerTools.js')
const { formatTime, formatTimeTwo } = require("../../utils/util")

Page({
  /**
   * 页面的初始数据
   */
  data: {
    isUpdate: 0,
    ledgerType: ledgerConfig().type,
    ledgerCategory: ledgerConfig().category,
    date_lebal: '',
    form: ledgerConfig().default
  },
  /*收支类型变更事件*/
  typeChange(e) {
    const onIndex = e.currentTarget.dataset.i
    var categoryDefault
    if (0 <= onIndex && onIndex < 2) {
      categoryDefault = this.data.ledgerCategory.findIndex(item => item.type == onIndex)
    } else { categoryDefault = 0 }
    this.setData({ "form.type": onIndex, 'form.category': categoryDefault })
  },
  /*日期变更*/
  pickerChangeDate(e) {
    this.setData({ 'form.date': e.detail.value, 'date_lebal': formatTimeTwo(e.detail.value, 'M月D日'), })
  },
  /*设置金额*/
  setMoney(e) {
    const pwd = e.detail.value
    const result = pwd.replace(/[^\d.]/g, '')
    this.setData({ ['form.money']: result })
  },
  /*设置备注*/
  setDesc(e) { this.setData({ ['form.desc']: e.detail.value }) },
  /*选择类别 */
  onSelectCategory(e) { this.setData({ ['form.category']: e.currentTarget.dataset.i }) },
  /*保存 */
  onSaveButton() {
    var { date, category, money, type } = this.data.form
    var code = true
    if (!date || date == 0) {
      code = false
      wx.showToast({ title: '日期不能为空', icon: 'none' })
    }
    if (category < 0 || category >= this.data.ledgerCategory.length) {
      code = false
      wx.showToast({ title: '类型不正确' + (!category), icon: 'none' })
    }
    money = parseInt(money)
    if (!money || money < 0) {
      code = false
      wx.showToast({ title: '无效的金额', icon: 'none' })
    }
    if (type < 0 || type > 2) {
      code = false
      wx.showToast({ title: '收支方式不正确', icon: 'none' })
    }
    if (code) {
      this.data.form.money = money
      ledgerTools.addLedger(this.data.form)
      wx.showToast({ title: '记下啦！', icon: "success" })
      this.setData({
        'form': ledgerConfig().default,
        'form.date': formatTimeTwo(formatTime(new Date()), 'Y-M-D'),
        'date_lebal': formatTimeTwo(formatTime(new Date()), 'M月D日'),
      })
    }
  },
  exitUpdata() {
    getApp().globalData.updateId = 0
    this.setData({
      isUpdate: 0, 'form': ledgerConfig().default,
      'form.date': formatTimeTwo(formatTime(new Date()), 'Y-M-D'),
      'date_lebal': formatTimeTwo(formatTime(new Date()), 'M月D日'),
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({ selected: 1 })
    }
    this.setData({
      'form.date': formatTimeTwo(formatTime(new Date()), 'Y-M-D'),
      'date_lebal': formatTimeTwo(formatTime(new Date()), 'M月D日'),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const uID = getApp().globalData.updateId
    if (uID != 0) {
      const oneData = ledgerTools.getDataById(uID)
      if (oneData) {
        this.setData({
          isUpdate: 1, form: oneData,
          date_lebal: formatTimeTwo(formatTime(new Date(oneData.date)), 'M月D日')
        })
      }
      getApp().globalData.updateId = 0
    } else {
      this.setData({
        isUpdate: 0, 'form': ledgerConfig().default,
        'form.date': formatTimeTwo(formatTime(new Date()), 'Y-M-D'),
        'date_lebal': formatTimeTwo(formatTime(new Date()), 'M月D日'),
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})